package com.sgb.kjwl.viewmodel.home;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.MaterialListEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.OptionFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OptionFragModel extends BaseViewModel {
    private HomeRepertory repertory;
    private OptionFragment fragment;

    public OptionFragModel(OptionFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getMaterialListModel(String cid, String areacode) {
        if (areacode.equals("")) {
            areacode = repertory.getAreaCode();
        }
        /*repertory.getMaterialList(cid, areacode)
                .subscribe(new Consumer<ListResponse<List<MaterialListEntity>>>() {
                    @Override
                    public void accept(ListResponse<List<MaterialListEntity>> listListResponse) throws Exception {
                        if (listListResponse != null) {
                            fragment.showOptionView(listListResponse.getList());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });*/

        APIManager.getInstance().getMaterialAPI().queryMaterailList(cid, areacode).enqueue(new Callback<BaseEntity<List<MaterialListEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MaterialListEntity>>> call, Response<BaseEntity<List<MaterialListEntity>>> response) {
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    fragment.showOptionView(response.body().getData());
                }else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MaterialListEntity>>> call, Throwable t) {
                MToast.showToast(fragment.getActivity(),t.getMessage());
            }
        });
    }
}
